1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.cache;
18
19 import static com.google.common.base.Preconditions.checkState;
20
21 import com.google.caliper.BeforeExperiment;
22 import com.google.caliper.Benchmark;
23 import com.google.caliper.Param;
24 import com.google.common.cache.LocalCache.ReferenceEntry;
25 import com.google.common.cache.LocalCache.Segment;
26
27 import java.util.concurrent.atomic.AtomicReferenceArray;
28
29
30
31
32
33
34 public class SegmentBenchmark {
35
36 @Param({"16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"}) int capacity;
37
38 private Segment<Object, Object> segment;
39
40 @BeforeExperiment
41 void setUp() {
42 LocalCache<Object, Object> cache = new LocalCache<Object, Object>(
43 CacheBuilder.newBuilder()
44 .concurrencyLevel(1)
45 .initialCapacity(capacity), null);
46 checkState(cache.segments.length == 1);
47 segment = cache.segments[0];
48 checkState(segment.table.length() == capacity);
49 for (int i = 0; i < segment.threshold; i++) {
50 cache.put(new Object(), new Object());
51 }
52 checkState(segment.table.length() == capacity);
53 }
54
55 @Benchmark int time(int reps) {
56 int dummy = 0;
57 AtomicReferenceArray<ReferenceEntry<Object, Object>> oldTable = segment.table;
58 for (int i = 0; i < reps; i++) {
59 segment.expand();
60 segment.table = oldTable;
61 dummy += segment.count;
62 }
63 return dummy;
64 }
65 }